<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue App</title>
  <!-- <script src="https://d.aardio.com/ajs/aardio.js"></script> -->
  <meta name="author" content="<?=owner.author?>">
  <!-- <script src="/aardio.js"></script> -->
  <script src="<?=owner.rpcUrl?>"></script>
  <!-- 引入 Vue.js -->
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
  <!-- 引入 Ant Design 的 CSS 文件 -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ant-design-vue@1.7.8/dist/antd.css">
  <!-- 引入 Ant Design 的 JavaScript 文件 -->
  <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@1.7.8/dist/antd.min.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      height: 100%;
      margin: 0;
    }

    .layout {
      background: rgba(255, 255, 255, 0.3);
    }
  </style>
</head>

<body>
  <div id="app">
    <a-layout class="layout">
      <a-layout-content style="padding: 1em;">
        <a-affix :offset-top="top">
          <a-row :gutter="[8,16]" type="flex" justify="center">
            <a-col flex="2px"></a-col>
            <a-col :span="8">
              <a-select v-model="selectedCountry" @change="updateCategories" style="width: 100%">
                <a-select-option disabled value="">请选择</a-select-option>
                <a-select-option v-for="country in countries" :value="country.href" :key="country.text">
                  {{ country.text }}</a-select-option>
              </a-select>
            </a-col>
            <a-col :span="8">
              <a-select v-model="selectedCategory" @change="updateImages" style="width: 100%">
                <a-select-option disabled value="">请选择</a-select-option>
                <a-select-option v-for="category in categories" :value="category.href" :key="category.text">
                  {{ category.text }}</a-select-option>
              </a-select>
            </a-col>
            <a-col :span="3">
              <a-button type="primary" @click="set_desktop" :loading="spinning" block>置为桌面</a-button>
            </a-col>
            <a-col :span="3">
              <a-button type="primary" @click="bat_download" :loading="spinning" block>批量下载</a-button>
            </a-col>
          </a-row>

          <a-row :gutter="[8,16]">
            <a-col :span="24">
              <img v-if="selectedImage" :src="selectedImage" alt="图片"
                style="max-width: 100%; max-height: 100%; height: auto;  ">
              <div v-else style="text-align: center;">
                <div><a-button type="text" @click="clean_cache" :loading="loading_clean_cache">清理缓存</a-button></div>
                <div><a-spin :spinning="spinning" tip="Loading..." size="large" style="margin-top: 4em;"/></div>                
              </div>
            </a-col>
          </a-row>
 
          <a-row :gutter="[8,16]"  v-if="spinning">
            <a-col :span="24"> 
            </a-col>
          </a-row> 

        </a-affix>
      </a-layout-content>
    </a-layout>
  </div>

  <script>
    var xx = "<?=owner.xx?>";
    if ("xx" != xx) {
      alert(xx);
    }

    // 注册 Ant Design 的组件
    Vue.use(antd);

    new Vue({
      el: '#app',
      data: {
        bizhi_url: "https://cn.bing.com/spotlight?spotlightId=BambooForestArashiyama&q=china&carindexpill=5&carindeximg=0&textorimgcar=text&isfullscreen=false&carscrlimgv2=0&form=SLVMPI&ssid=&trsid=NONE&chevclk=false",
        countries: [], // 国家列表
        categories: [], // 类别列表
        subcategories: [], // 子类别列表
        selectedCountry: '', // 选中的国家
        selectedCategory: '', // 选中的类别
        selectedSubcategory: '', // 选中的子类别
        images: [], // 图片列表
        selectedImage: '', // 选中的图片URL
        loading_clean_cache: false, // 是否显示清理缓存动画
        spinning: true // 是否显示加载动画
      },
      methods: {
        get_time() {
          aardio.time().then(response => {
            console.log("time", response);
          });
        },
        clean_cache() {
          this.loading_clean_cache = true;
          aardio.clean_cache().then(response => {
              console.log("clean_cache", response);
            }).then(() => {
              this.get_time();
            }).then(() => {
              setTimeout(() => {
                // location.reload();
                this.fetchCountries();
              }, 1000);
            }).catch(error => {
              console.error('clean_cache:', error);
            }).finally(() => {
              this.loading_clean_cache = false;
            });

        },
        set_desktop() { //设置当前图片为桌面背景
          this.spinning = true;
          aardio.desktop(this.selectedImage).then(response => {
            console.log("set_desktop", response);
            // alert("set_desktop");            
          }).catch(error => {
            this.selectedImage = "";
            console.error('set_desktop:', error);
          }).finally(() => {
            this.spinning = false;
          });
        },
        bat_download() { ///批量下载当前国家图片
          this.spinning = true;
          aardio.download(this.selectedCountry).then(response => {
            console.log("bat_download", response);
            // alert("bat_download");     
          }).catch(error => {
            console.error('bat_download:', error);
          }).finally(() => {
            this.spinning = false;
          });
        },
        updateImages() {
          this.spinning = true;
          aardio.images(this.selectedCategory).then(response => {
            console.log("images", response);
            this.selectedImage = response;
          }).then(() => {}).catch(error => {
            console.error('获取类别数据失败images:', error);
          }).finally(() => {
            this.spinning = false;
          });

        },
        updateCategories() {
          this.spinning = true;
          aardio.categories(this.selectedCountry).then(response => {
            console.log("categories", response);
            this.categories = response;
            this.selectedCategory = this.categories[0].href; // 默认选择第一行
          }).then(() => {
            var tttupdateImages = setInterval(() => {
              if ("" == this.selectedCategory) {} else {
                this.updateImages();
                clearInterval(tttupdateImages);
              }
            }, 400);
          }).catch(error => {
            console.error('获取类别数据失败categories:', error);
          }).finally(() => {
            this.spinning = false;
          });

        },
        fetchCountries() {
          this.spinning = true;
          aardio.countries(this.bizhi_url).then(response => {
            console.log("countries", response);
            this.countries = response;
            this.selectedCountry = this.countries[0].href; // 默认选择第一行             
          }).then(() => {
            var tttupdateCategories = setInterval(() => {
              if ("" == this.selectedCountry) {} else {
                this.updateCategories();
                clearInterval(tttupdateCategories);
              }
            }, 400);
          }).catch(error => {
            console.error('获取类别数据失败countries:', error);
          }).finally(() => {
            this.spinning = false;
          });
        }
      },
      // 实例创建完成后调用
      created() {
        setTimeout(() => {
          this.fetchCountries();
        }, 100);
      }
    });
  </script>
</body>

</html>